package com.kcsj.service;


import com.kcsj.dao.InventoryDao;
import com.kcsj.dao.ProductModelDao;
import com.kcsj.dao.WarehouseInputRecordDao;
import com.kcsj.domain.Inventory;
import com.kcsj.domain.WarehouseInputRecord;
import com.kcsj.util.Condition;
import com.kcsj.util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public class WarehouseInputRecordService {
    private static WarehouseInputRecordService warehouseInputRecordService = new WarehouseInputRecordService();
    public static WarehouseInputRecordService getInstance(){return warehouseInputRecordService;}
    private WarehouseInputRecordDao warehouseInputRecordDao = WarehouseInputRecordDao.getInstance();
    private ProductModelDao productModelDao = ProductModelDao.getInstance();
    private InventoryDao inventoryDao = InventoryDao.getInstance();

    public WarehouseInputRecord find(int id) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        try{
            WarehouseInputRecord warehouseInputRecord;
            warehouseInputRecord = warehouseInputRecordDao.find(id,conn);
            return warehouseInputRecord;
        }catch (SQLException e){
            conn.rollback();
            throw e;
        }finally {
            conn.setAutoCommit(true);
        }
    }

    public Collection<WarehouseInputRecord> findAll(List<Condition> conditionList) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        conn.setAutoCommit(false);
        try {
            Collection<WarehouseInputRecord> warehouseInputRecords;
            warehouseInputRecords = warehouseInputRecordDao.findAll(conditionList, conn);
            return warehouseInputRecords;
        } catch (SQLException e) {
            e.printStackTrace();
            conn.rollback();
            throw e;
        } finally {
            conn.setAutoCommit(true);
        }
    }


    public int add(WarehouseInputRecord warehouseInputRecord,Integer amount) throws SQLException {
        //获取连接
        Connection conn = JdbcHelper.getConn();
        try {
            //取消自动提交，开始事务
            conn.setAutoCommit(false);
            String productModelNo = warehouseInputRecord.getProductModel().getProductModelNo();
            Inventory inventory = new Inventory();
            inventory.setProductModel(warehouseInputRecord.getProductModel());
            inventory.setWarehouse(warehouseInputRecord.getProductModel().getWarehouse());
            inventory.setAmount(amount);

            //调用dao的增加方法
            int affectedRows = warehouseInputRecordDao.add(warehouseInputRecord,conn);
            productModelDao.changeRest(productModelNo, 1, conn);
            inventoryDao.update(inventory, conn);
            //返回主键，为0则说明仍未正确插入
            return affectedRows;
        } catch (SQLException e) {
            conn.rollback();
            throw e;
        } finally {
            conn.setAutoCommit(true);
        }
    }

    public boolean addAll(List<WarehouseInputRecord> warehouseInputRecords) throws SQLException {
        //获取连接
        Connection conn = JdbcHelper.getConn();
        try {
            //取消自动提交，开始事务
            conn.setAutoCommit(false);
            //调用dao的增加方法
            int count = warehouseInputRecordDao.addAll(warehouseInputRecords,conn);
            return true;
        } catch (SQLException e) {
            conn.rollback();
            throw e;
        } finally {
            conn.setAutoCommit(true);
        }
    }

}
